package com.techsen.tsweb.sys.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.sql.Pager;
import com.techsen.tsweb.sys.domain.Person;
import com.techsen.tsweb.sys.service.OrgService;
import com.techsen.tsweb.sys.service.PersonService;

@Controller
@RequestMapping("/sys/person")
public class PersonController {
    
    @Resource
    private PersonService personService;
    
    @Resource
    private OrgService orgService;
    
    /**
     * 根据ID查询人员
     */
    @RequestMapping("/orgid/{orgId}")
    public String view(@PathVariable("orgId") String orgId, Model model) {
        Person person = this.personService.getOneByEntity(new Person().setOrgId(orgId));
        model.addAttribute("person", person);
        return "sys/person-view";
    }
    
    /**
     * 修改
     */
    @RequestMapping("/update")
    public String update(Person person) {
        this.personService.update(person);
        return "redirect:" + person.getId();
    }
    
    /**
     * 进入人员管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/person";
    }
    
    /**
     * 分页查询
     */
    @RequestMapping("/pager")
    public @ResponseBody Pager<Person> pager(@RequestBody Pager<Person> pager) {
        return this.personService.findByPager(pager);
    }
    
    /**
     * 进入添加界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "sys/person-add";
    }
    
    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(Person person) {
        this.personService.add(person);
        return new JsonResult().setEntity(person);
    }
    
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        this.personService.delete(id);
        return new JsonResult(id);
    }
    
    /**
     * 进入修改界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String personId, Model model) {
        Person person = this.personService.get(personId);
        model.addAttribute("person", person);
        return "sys/person-update";
    }
    
    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult ajaxUpdate(Person person) {
        this.personService.update(person);
        return new JsonResult(person);
    }
    
    /**
     * 根据人员名查询人员集合
     */
    @RequestMapping("/persons-like-name")
    public @ResponseBody List<Person> personsLikeName(
            @RequestParam(value = "q", required = false) String personName) {
        return this.personService.getPersonsLikeName(personName);
    }
    
}
